package com.knowvoyage.knowvoyageserver.service.Impl;

import com.knowvoyage.knowvoyageserver.mapper.MessageMapper;
import com.knowvoyage.knowvoyageserver.pojo.entity.Message;
import com.knowvoyage.knowvoyageserver.service.AiChatService;
import com.knowvoyage.knowvoyageserver.util.ChatClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class AiChatServiceImpl implements AiChatService {

    private final MessageMapper messageMapper;

    @Override
    public Mono<String> load(Long dialogId, Integer mode) {
        List<Message> messages = messageMapper.selectByDialogId(dialogId);
        List<LinkedHashMap<String,String>> list = new ArrayList<>();
        for (Message message : messages) {
            LinkedHashMap<String,String> askMap = new LinkedHashMap<>();
            askMap.put("role","user");
            askMap.put("content",message.getAsk());
            list.add(askMap);

            LinkedHashMap<String,String> replyMap = new LinkedHashMap<>();
            replyMap.put("role","assistant");
            replyMap.put("content",message.getReply());
            list.add(replyMap);
        }

        LinkedHashMap<String, Object> body = new LinkedHashMap<>();
        body.put("dialogId", dialogId.toString());
        body.put("messages", list);
        body.put("mode", mode.toString());
        return new ChatClient().loadMessages(body);
    }
}
